﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习八月份
{
    public class Solution438
    {
        public IList<int> FindAnagrams(string s, string p)
        {
            IList<int> res = new List<int>();
            int lenp = p.Length;
            int lens = s.Length;
            Dictionary<char, int> mapp = new Dictionary<char, int>();
            Dictionary<char, int> maps = new Dictionary<char, int>();
            for (int i = 0; i < lenp; i++)
            {

                if (mapp.ContainsKey(p[i]))
                {
                    mapp[p[i]]++;

                }
                else
                {
                    mapp[p[i]] = 1;
                }

                if (maps.ContainsKey(s[i]))
                {
                    maps[s[i]]++;

                }
                else
                {
                    maps[s[i]] = 1;
                }

            }
            if (IsAnagrams(maps, mapp))
            {
                res.Add(0);
            }
            for (int start = 0; start < lens - lenp ; start++)
            {

                char curS = s[start];

                maps[curS]--;
                char nextS = s[start+ lenp];
                if (maps.ContainsKey(nextS))
                {
                    maps[nextS]++;

                }
                else
                {
                    maps[nextS] = 1;
                }


                if (IsAnagrams(maps, mapp))
                {
                    res.Add(start);
                }

            }
            return res;

        }

        public bool IsAnagrams(Dictionary<char, int> maps, Dictionary<char, int> mapp)
        {
           

            foreach (char ch in mapp.Keys)
            {
                Console.WriteLine(ch);
                if (maps.ContainsKey(ch))
                {
                   

                    if (maps[ch] != mapp[ch])
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }

            }
            return true;

        }
    }

    class Program438
    {

        static void Main438(string[] args)
        {

            Solution438 sol = new Solution438();
            string s = "cbaebabacd", p = "abc";
            IList<int> res= sol.FindAnagrams(s, p);
            for(int i = 0;i < res.Count;i++)
            {
                Console.WriteLine(res[i]);
            }

        }
    }
}
